package com.spider.silence.pool;

import com.google.common.collect.Lists;
import com.spider.silence.downloader.SinaWeiBoDownLoader;
import com.spider.silence.utils.XmlUtil;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @author liyuanguo
 * @version V1.0
 * @time 2017/3/2 9:21
 * @description
 */
public class SinaWeiBoDownLoaderPool {
    private Logger logger = Logger.getLogger(SinaWeiBoDownLoaderPool.class);
    private static  SinaWeiBoDownLoaderPool instance=new SinaWeiBoDownLoaderPool();;
    protected LinkedList<SinaWeiBoDownLoader> pool = Lists.newLinkedList();


    private  SinaWeiBoDownLoaderPool(){
        Document document = XmlUtil.getConf("META-INF/SinaWeiBo-conf.xml");
        final List<Element> elements=document.getRootElement().element("login-user-list").elements();
        Thread thread =new Thread(new Runnable() {
            @Override
            public void run() {
                List<String[]> list = Lists.newArrayList();
                for(int i=0;i<elements.size();i++){
                    try {
                        Element e = elements.get(i);
                        String qi=e.element("login-id").getText();
                        String pwd=e.element("pwd").getText();
                        String[] info=new String[]{qi,pwd};
                        list.add(info);
                    }  catch (Exception e) {
                        e.printStackTrace();
                        logger.error(e.getMessage(),e);
                    }
                }
                Collections.shuffle(list);//打乱登录顺序
                for(int i=0;i<list.size();i++){
                    String[] info=list.get(i);
                    SinaWeiBoDownLoader sinaWeiBoDownLoader= null;
                    sinaWeiBoDownLoader=new SinaWeiBoDownLoader(info[0],info[1]);
                    boolean flag=sinaWeiBoDownLoader.init();
                    if(flag){
                        synchronized (pool){
                            pool.add(sinaWeiBoDownLoader);
                            pool.notifyAll();
                        }
                    }
                }
            }
        });
        thread.start();
    }


    public  static SinaWeiBoDownLoaderPool getInstance(){
        return  instance;
    }
    public  void releaseDownLoader(SinaWeiBoDownLoader downloader) {
        if (downloader != null) {
            synchronized (pool) {
                pool.addLast(downloader);
                pool.notifyAll();
            }
        }
    }


    public  SinaWeiBoDownLoader fetchDownLoader(long mills) throws InterruptedException {
        synchronized (pool){
            if(mills<=0){
                while (pool.isEmpty()){
                    pool.wait();
                }
                return pool.removeFirst();
            }else {
                long future= System.currentTimeMillis()+mills;
                long remianing=mills;
                while (pool.isEmpty() && remianing >0){
                    pool.wait(remianing);
                    remianing=future-System.currentTimeMillis();
                }
                SinaWeiBoDownLoader downLoader =null;
                if(!pool.isEmpty()){
                    downLoader=pool.removeFirst();
                }
                return downLoader;
            }
        }
    }


    public  SinaWeiBoDownLoader fetchDownLoader() throws InterruptedException {
        synchronized (pool){
            while (pool.isEmpty()){
                pool.wait();
            }
            return pool.removeFirst();
        }
    }


    public void empty(){
        pool=Lists.newLinkedList();
    }
}
